package com.pes.arcww.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pes.arcww.service.UsersService;
import com.pes.arcww.controller.UtilsApp;
import com.pes.arcww.form.Users;

@Controller
public class InitController {

	private static final Logger logger = LoggerFactory
			.getLogger(InitController.class);
	private static UtilsApp utils = new UtilsApp();

	@Autowired(required = true)
	private UsersService userService;

	// CONSTANTS
	private final String SRC_PATH_HOME = "/home/";
	private final String SRC_PATH_ACCOUNT = "/account/";

	@RequestMapping(value = "/" , method = RequestMethod.GET)
    public String redirectLogin(HttpServletResponse httpServletResponse) {
		return "redirect:/login_input";
    }
	
	@RequestMapping(value = "/login_input", method = RequestMethod.GET)
	public @ResponseBody
	ModelAndView login_input(@ModelAttribute("model") ModelMap model,
			BindingResult result, HttpServletRequest request) {

		ModelAndView mav = new ModelAndView();
		try{
			if(request.getSession().getAttribute("logged_user") == null){
				model = utils.init(request, model, userService);
				mav = new ModelAndView(SRC_PATH_HOME + "index");
				mav.addObject("m_site", model);
			}else{
				//redirect to my account
				mav=new ModelAndView("redirect:/account");
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return mav;
	}
	
	@RequestMapping(value = "/login" , method = RequestMethod.POST)
	public @ResponseBody
	ModelAndView login(@ModelAttribute("model") ModelMap model,
			BindingResult result, HttpServletResponse response,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String email;
		String password;
		
		try{	
			model = utils.init(request, model, userService);
			email 	= request.getParameter("email").toString();
			password = request.getParameter("password").toString();
			
			
			Users user = userService.getUser(email);
			if(user.getEmail() == null){
				model.addAttribute("msgErr", "User does not exist. Please provide valid data.");
				mav = new ModelAndView(SRC_PATH_HOME + "index");
			}else{
				model.addAttribute("m_email", email);
				if(!userService.validate(email, password)){
					model.addAttribute("msgErr", "Wrong information provided. Please check your password.");
					mav = new ModelAndView(SRC_PATH_HOME + "index");
				}else{	
					request.getSession().setAttribute("logged_user", user.getEmail());
					
					//redirect to my account
					mav=new ModelAndView("redirect:/account");
				}
			}	
			
		}catch(Exception e){
			model.addAttribute("msgErr", "User does not exist. Please provide valid data.");
			mav = new ModelAndView(SRC_PATH_HOME + "index");
		}
		
		mav.addObject("m_site", model);

		return mav;
	}
	
	@RequestMapping(value = "/account" , method = RequestMethod.GET)
	public @ResponseBody
	ModelAndView account(@ModelAttribute("model") ModelMap model,
			BindingResult result, HttpServletRequest request) {
		
		ModelAndView mav = new ModelAndView();
		
		try{
			model = utils.init(request, model, userService);
			if(request.getSession().getAttribute("logged_user") == null){
				mav = new ModelAndView("redirect:/");
				return mav;
			}else{
				model = utils.init(request, model, userService);
				mav = new ModelAndView(SRC_PATH_ACCOUNT + "account");
				mav.addObject("m_site", model);
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return mav;
	}
	
	@RequestMapping(value = "/logout" , method = RequestMethod.GET)
	public @ResponseBody
	ModelAndView logout(@ModelAttribute("model") ModelMap model,
			BindingResult result, HttpServletRequest request) {
		
		logger.debug("logout( model, result, request)");
		ModelAndView mav = new ModelAndView();
		
		try{
			model = utils.init(request, model, userService);
			
			//destroy session
			request.getSession().invalidate();
			
			mav = new ModelAndView("redirect:/");
			mav.addObject("m_site", model);
		}catch(Exception e){
			logger.debug("logout( model, result, request) -" + e.getMessage());
		}
		return mav;
	}
}
